package com.solution.admin.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.customer.entity.CompanyExpressConfigEntity;
import com.solution.admin.customer.entity.CustomerEntity;
import com.solution.admin.customer.entity.DeliveryAddressEntity;
import com.solution.admin.customer.service.CompanyExpressConfigAddressService;
import com.solution.admin.customer.service.CompanyExpressConfigService;
import com.solution.admin.customer.service.CustomerService;
import com.solution.admin.customer.service.DeliveryAddressService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 收货地址表web控制层
 * @author solin
 * @date 2017-09-22 11:55:25
 */
@RequestMapping("/deliveryAddress")
@Controller
public class DeliveryAddressController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(DeliveryAddressController.class);

	@Autowired
	private DeliveryAddressService deliveryAddressService;
	
	@Autowired
	private CompanyExpressConfigService companyExpressConfigService;
	
	@Autowired
	private CompanyExpressConfigAddressService companyExpressConfigAddressService;
	
	@Autowired
	private CustomerService customerService;
	
	/**
	 * 跳转收货地址表查询页面
	 * @param DeliveryAddress 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-22 11:55:25
	 */
	@RequestMapping("/toListDeliveryAddressPage")
	public String toListDeliveryAddressPage(DeliveryAddressEntity deliveryAddress, HttpServletRequest request) {
		return "customer/deliveryAddress_list";
	}

	/**
	 * 分页查询收货地址表列表
	 * @param deliveryAddress 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-09-22 11:55:25
	 */
	@RequestMapping("/listDeliveryAddressPage")
	@ResponseBody
	public JSONGrid listDeliveryAddressPage(DeliveryAddressEntity deliveryAddress, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			paramMap.put("customerName", request.getParameter("customerName"));
			paramMap.put("companyName", request.getParameter("companyName"));
			paramMap.put("receiverName", request.getParameter("receiverName"));
			paramMap.put("mobile", request.getParameter("mobile"));
			paramMap.put("receiverAddr", request.getParameter("receiverAddr"));
			
			PageBean pageBean = deliveryAddressService.listDeliveryAddressPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询收货地址表失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-22 11:55:25
	 */
	@RequestMapping("/toAddDeliveryAddress")
	public String toAddDeliveryAddress(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/deliveryAddress_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-09-22 11:55:25
	 */
	@RequestMapping("/toUpdateDeliveryAddress")
	public String toUpdateDeliveryAddress(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			DeliveryAddressEntity deliveryAddress = deliveryAddressService.getDeliveryAddressById(id);
			request.setAttribute("deliveryAddress", deliveryAddress);
		} catch (Exception e) {
			handleException("跳转编辑收货地址表页面错误", logger, e);
		}
		return "customer/deliveryAddress_form";
	}


	/**
	 * 新增或保存收货地址表
	 * @param request 请求对象
	 * @param deliveryAddressEntity 收货地址表实体
	 * @return
	 * @author solin
 	 * @date 2017-09-22 11:55:25
	 */
	@RequestMapping("/saveOrUpdateDeliveryAddress")
	@ResponseBody
	public Map<String, Object> saveDeliveryAddress(HttpServletRequest request, DeliveryAddressEntity deliveryAddressEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			Long customerId = deliveryAddressEntity.getCustomerId();
			if(!StringUtil.isNotNull(customerId)){
				customerId = getCustomerId();
			}
			CustomerEntity customerEntity  = customerService.getCustomerById(customerId);
			deliveryAddressEntity.setCompanyId(customerEntity.getCompanyId());
			if(StringUtil.isNotEmpty(id)){
				result = deliveryAddressService.updateDeliveryAddress(deliveryAddressEntity);
				resultMap.put("deliveryAddressId", id);
			}else{
				deliveryAddressEntity.setCreater(getUserCode());
				if (deliveryAddressEntity.getCustomerId() == null || deliveryAddressEntity.getCustomerId().equals(0L)) {
					deliveryAddressEntity.setCustomerId(getCustomerId());
				}
				
				long deliveryId = deliveryAddressService.saveDeliveryAddress(deliveryAddressEntity);
				resultMap.put("deliveryAddressId", deliveryId);
				result = (int)deliveryId;
			}
			
			resultMap.put(PublicStatus.RESULT_CODE, result >= 1?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, result >= 1?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("添加收货地址表失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
			
		}
		return resultMap;
	}
	
	/**
	 * 该地址是否免费(任意一种免运费检查通过就算免运费)
	 * @param request
	 * @return
	 * @author xkl
	 * @date 2018年3月23日 下午4:35:58
	 */
	@RequestMapping("/isFreeExpressAddress")
	@ResponseBody
	public Map<String, Object> isFreeExpressAddress(HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String addressId = request.getParameter("addressId");
		Long companyId = getCompanyId();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("addressId", addressId);
		paramMap.put("companyId", companyId);
		List<CompanyExpressConfigEntity> expressList = companyExpressConfigService.listCompanyExpressConfig(paramMap);
		if (null != expressList && expressList.size() != 0) {
			//两种免运费类型都需要检查
			for (CompanyExpressConfigEntity entity : expressList) {
				if (entity.getExpressType() == 2 || entity.getExpressType() == 4) {
					resultMap.put(PublicStatus.RESULT_CODE, "1");
					resultMap.put(PublicStatus.RESULT_MSG, "免运费地址");
					return resultMap;
				} else {
					int count = companyExpressConfigAddressService.countByCompanyIdAndAddressId(paramMap);
					if (count > 0) {
						resultMap.put(PublicStatus.RESULT_CODE, "1");
						resultMap.put(PublicStatus.RESULT_MSG, "免运费地址");
						return resultMap;
					}
				}
			}
		}
		resultMap.put(PublicStatus.RESULT_CODE, "0");
		resultMap.put(PublicStatus.RESULT_MSG, "普通地址");
		return resultMap;
	}
	
	/**
	 * 获取地址列表
	 * @param request
	 * @param deliveryAddressEntity
	 * @return
	 * @author: solin
	 * @date: 2018年3月23日 上午10:41:11
	 */
	@RequestMapping("/listDeliveryAddress")
	@ResponseBody
	public Map<String, Object> listDeliveryAddress(HttpServletRequest request, DeliveryAddressEntity deliveryAddressEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try{
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("companyId", request.getParameter("companyId"));
			paramMap.put("customerId", request.getParameter("customerId"));
			List<DeliveryAddressEntity> deliveryAddressList = deliveryAddressService.listDeliveryAddress(paramMap);
			
			resultMap.put("data", deliveryAddressList);
			resultMap.put(PublicStatus.RESULT_CODE, deliveryAddressList != null?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, deliveryAddressList != null?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("获取地址列表失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		return resultMap;
	}
	
	/**
	 * 删除地址
	 * @param request
	 * @param deliveryAddressEntity
	 * @return
	 * @author: solin
	 * @date: 2017年11月2日 上午12:37:47
	 */
	@RequestMapping("/removeDeliveryAddress")
	@ResponseBody
	public Map<String, Object> removeDeliveryAddress(HttpServletRequest request, DeliveryAddressEntity deliveryAddressEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = deliveryAddressService.deleteDeliveryAddressById(deliveryAddressEntity.getId());
			}else{
				result = 0;
			}
			
			resultMap.put(PublicStatus.RESULT_CODE, result >= 1?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, result >= 1?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("删除收货地址表失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
			
		}
		return resultMap;
	}
	
}
